package com.microblogforsim.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import com.microblogforsim.R;
import com.microblogforsim.activity.PublishActivity;
import com.microblogforsim.adapter.FindPagerAdapter;
import com.microblogforsim.adapter.IconAdapter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.TimeZone;

public class Fragment_Publish extends Fragment implements View.OnTouchListener, View.OnClickListener {
    private View view;
    //    关闭按钮
    private ImageView btn_close;
    //    Pager容器
    private ViewPager pager;
    //    Pager里面view的列表
    private ArrayList<View> views;
    //    Pager适配器
    private FindPagerAdapter pagerAdapter;
    //    日期
    private TextView tv_day,tv_month,tv_week;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_publish,container,false);
        view.setOnTouchListener(this);
        init();
        return view;
    }

    private void init() {
        btn_close = view.findViewById(R.id.publish_close);
        pager = view.findViewById(R.id.fragment_find_pager);
        tv_day = view.findViewById(R.id.publish_day);
        tv_month = view.findViewById(R.id.publish_month);
        tv_week = view.findViewById(R.id.publish_week);

        views = new ArrayList<>();
        btn_close.setOnClickListener(this);

        setViewPager();
        setDate();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.publish_close:
                close();
                break;
        }
    }

    private void close() {
        FragmentManager fragmentManager = getFragmentManager();
        Fragment fragment = fragmentManager.findFragmentByTag("add");
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.remove(fragment);
        fragmentTransaction.commit();
    }

    private void setDate(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        int year    = calendar.get(Calendar.YEAR);
        int month   = calendar.get(Calendar.MONTH) + 1;
        int day     = calendar.get(Calendar.DAY_OF_MONTH);
        tv_day.setText(day+"");
        tv_month.setText(month+"/"+year);
        String week = "";
        switch (calendar.get(Calendar.DAY_OF_WEEK)-1){
            case 1:
                week = "星期一";
                break;
            case 2:
                week = "星期二";
                break;
            case 3:
                week = "星期三";
                break;
            case 4:
                week = "星期四";
                break;
            case 5:
                week = "星期五";
                break;
            case 6:
                week = "星期六";
                break;
            case 7:
                week = "星期七";
                break;
        }
        tv_week.setText(week);
    }

    private void setViewPager(){
        IconAdapter adapter1,adapter2;
        ArrayList<Icon> icons1 = new ArrayList<>(), icons2 = new ArrayList<>();
        LayoutInflater inflater = getLayoutInflater();
        View view_one = inflater.inflate(R.layout.view_pager_find_1,null);
        View view_two = inflater.inflate(R.layout.view_pager_find_2,null);
        GridView gridView_1 = view_one.findViewById(R.id.view_page_find_1_list);
        GridView gridView_2 = view_two.findViewById(R.id.view_page_find_2_list);

        icons1.add(new Icon(R.drawable.ic_wenzi,"文字",R.drawable.bg_100_ping));
        icons1.add(new Icon(R.drawable.ic_xiangji,"拍摄",R.drawable.bg_100_green));
        icons1.add(new Icon(R.drawable.ic_xiangce,"相册",R.drawable.bg_100_blue));
        icons1.add(new Icon(R.drawable.ic_wenda,"问答",R.drawable.bg_100_yellow));
        icons1.add(new Icon(R.drawable.ic_weizhi,"签到",R.drawable.bg_100_green));
        icons1.add(new Icon(R.drawable.ic_dianping,"点评",R.drawable.bg_100_blue));
        icons1.add(new Icon(R.drawable.ic_chuangjianhuati,"话题",R.drawable.bg_100_blue));
        icons1.add(new Icon(R.drawable.ic_haoyouquan,"好友圈",R.drawable.bg_100_red));

        icons2.add(new Icon(R.drawable.ic_huati,"未知",R.drawable.bg_100_green));
        icons2.add(new Icon(R.drawable.ic_huati,"未知",R.drawable.bg_100_green));

        adapter1 = new IconAdapter(getContext(),icons1);
        adapter2 = new IconAdapter(getContext(),icons2);
        gridView_1.setAdapter(adapter1);
        gridView_2.setAdapter(adapter2);
        views.add(view_one);
        views.add(view_two);
        pagerAdapter  = new FindPagerAdapter(views);
        pager.setAdapter(pagerAdapter);

        gridView_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    default:
                        startActivity(new Intent(getContext(), PublishActivity.class));
                }
            }
        });

        gridView_2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    default:
                        startActivity(new Intent(getContext(), PublishActivity.class));
                }
            }
        });
    }
}
